package net.sky.oa.dao.infopub.impl;

import java.util.ArrayList;
import java.util.List;

import net.sky.oa.dao.SkyBaseDaoImpl;
import net.sky.oa.dao.infopub.InfNoticeDao;
import net.sky.oa.model.InfNotice;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.htsoft.core.web.paging.PagingBean;

@SuppressWarnings("unchecked")
@Repository
public class InfNoticeDaoImpl extends SkyBaseDaoImpl<InfNotice> implements InfNoticeDao {

	public InfNoticeDaoImpl() {
		super.setPersistType(InfNotice.class);
	}
	
	public List<InfNotice> getDisplay(PagingBean pb,Integer status,Integer sort) {
		ArrayList params = new ArrayList();
		StringBuffer hql = new StringBuffer("select new InfNotice(n.id,n.title,n.istop,n.createtime,n.user) from InfNotice n where 1=1");
		if(null!=status)
		{
			hql.append(" and n.status ="+status);
		}
		if(null!=sort)
		{
			hql.append(" and n.sort ="+sort);
		}
		hql.append(" order by  n.istop asc, n.createtime desc");
		return findByHql(hql.toString(), params.toArray(), pb);
	}
	/**
	 * 1.查询公司公告,需求:根据title,user[name],rank,createTime(begin、end),type查所有status为1的公告  sort=2
	 * 2.查询发布公告,需求:根据title,rank,type查我的(current user)公告  sort=2
	 * 3.查询公司通知,需求:根据title,user[name],rank,createTime(begin、end),type查所有status为1且发给我通知  sort=1
	 * 4.查询发布通知,需求:根据title,rank,type查我的(current user)发出的通知  sort=1
	 * 5.查询公告管理,需求:根据title,userName,rank,createTime(begin、end),type查所有status为1的公告  sort=2
	 * 6.查询通知管理,需求:根据title,userName,rank,createTime(begin、end),type查所有status为1的通知  sort=1
	 */
	public List<InfNotice> findByParams(String title,String userName,Integer rank,Long type,Integer sort,Long userId,Integer opType,String beginTime,String endTime,PagingBean pb) 
	{
		try {
			List list=new ArrayList();
			StringBuffer sql=new StringBuffer("");
			if(opType==1)//查询公司通知
			{
				sql.append("select infu.notice from InfNoticeUser " +
				 		" infu where 1=1 and infu.notice.status=1 and infu.notice.sort=1");
				 if(userId!=-1)
				 {
					 sql.append(" and infu.employee.id= ?");
					 list.add(userId);
				 }
				 if(null!=title && !"".equals(title))
				 {
					 sql.append(" and infu.notice.title like ?");
					 list.add("%"+title+"%");
				 }
				 if(StringUtils.isNotEmpty(userName)){
					 sql.append(" and infu.notice.user.name like ?");
					 list.add("%"+userName+"%");
				 }
				 if(rank!=-1){
					 sql.append(" and infu.notice.rank= ?");
					 list.add(rank);
				 }
				 if(StringUtils.isNotEmpty(beginTime))
				 {
					 sql.append(" and to_char(infu.notice.createtime,'yyyy-MM-dd') >=  ? ");
					 list.add(beginTime);
				 }
				 if(StringUtils.isNotEmpty(endTime))
				 {
					 sql.append(" and to_char(infu.notice.createtime,'yyyy-MM-dd') <=  ? ");
					 list.add(endTime);
				 }
				 if(type!=-1){
					 sql.append(" and infu.notice.type.id= ?");
					 list.add(type);
				 }
				 sql.append(" order by infu.notice.istop asc,infu.notice.createtime desc");
			}
			if(opType==2||opType==3)
			{
				 sql.append("select inf from InfNotice inf where 1=1 ");
				 if(userId!=-1)
				 {
					 sql.append(" and inf.user.id= ?");
					 list.add(userId);
				 }
				 if(StringUtils.isNotBlank(title))
				 {
					 sql.append(" and inf.title like ?");
					 list.add("%"+title+"%");
				 }
				 if(StringUtils.isNotBlank(userName)){
					 sql.append(" and inf.user.name like ?");
					 list.add("%"+userName+"%");
				 }
				 if(rank!=-1){
					 sql.append(" and inf.rank= ?");
					 list.add(rank);
				 }
				 if(sort!=-1){
					 sql.append(" and inf.sort= ?");
					 list.add(sort);
				 }
				 if(type!=-1){
					 sql.append(" and inf.type.id= ?");
					 list.add(type);
				 }
				 if(StringUtils.isNotBlank(beginTime))
				 {
					 sql.append(" and to_char(inf.createtime,'yyyy-MM-dd') >  ? ");
					 list.add(beginTime);
				 }
				 if(StringUtils.isNotBlank(endTime))
				 {
					 sql.append(" and to_char(inf.createtime,'yyyy-MM-dd') <  ? ");
					 list.add(endTime);
				 }
				 sql.append(" order by inf.istop asc,inf.createtime desc");
			}
			return findByHql(sql.toString(), list.toArray(), pb);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
